//对主题贴加精或取消加精
function good_set(domid, request_url) {
	$.ajax({
        type:"POST",
        dataType:'json',
        url:request_url,
        success:function(data){
        	if(data.status == '1') {
        		art.dialog({
	        	    icon: 'succeed',
	        	    content: data.info,
	        	    time:2
	        	});
        		location.reload(true);
        		/*if(data.data.type == 'good') {
        			$("#"+domid).html('<i class="icon icon-star"></i>取消加精');
        		} else if(data.data.type == 'cancel_good') {
        			$("#"+domid).html('<i class="icon icon-star-empty"></i>加精');
        		}*/
        	} else {
        		art.dialog({
	        	    icon: 'error',
	        	    content: data.info,
	        	    time:2
	        	});
        	}
        },
        error:function(data){
        	art.dialog({
        	    icon: 'error',
        	    content: '操作失败！',
        	    time:2
        	});
        }
    });
}

//回复页面基本js代码
$(function() {
	var form = $('.media:first');
	//插入回复表单
	$('.media-list').delegate('.reply-btn','click',function(event){
		var parent = $(this).closest('.media-body');
		//写入对应回复ID
		form.find(':hidden[name=parentid]').val($(this).attr('parentId'));
		var textarea = parent.append(form).find('textarea');
		//对回复回复的处理
		var username = $(this).parents('.toolbar').siblings('p').find('a.username').text();
		textarea.val('回复 @' + username);
		moveEnd(textarea.get(0));
		event.stopPropagation();
	});
	
	$('.media-list').delegate('.media-body','click',function(event){
		$(this).has('form').length && event.stopPropagation();
	});
	
	//点击评论框以外的地方，询问是否重置评论表单
	
		/*$("textarea[name=content]").blur(function() {
			var flag = false;
			$(".reply-button").click(function(){ 
				alert('test');
				flag = true;
			});
			if(form.find(':hidden[name=parentid]').val() != 0) {
				art.dialog.confirm("你编辑的内容尚未保存，确定要离开吗？", function(){
					$('#comment').after(form);
					form.find(':hidden[name=parentid]').val(0);
					form.find('textarea').val('');
			    }, function() {
			    	moveEnd($("textarea[name=content]").get(0));
			    });
			}
		});
		alert(flag);*/
	
});

//将光标移动到textarea末尾
function moveEnd(obj){
	obj.focus();
	var len = obj.value.length;
	if (document.selection) {
		var sel = obj.createTextRange(); 
		sel.moveStart('character',len); 
		sel.collapse(); 
		sel.select(); 
	} else if (typeof obj.selectionStart == 'number' && typeof obj.selectionEnd == 'number') {
		obj.selectionStart = obj.selectionEnd = len; 
	} 
}
		
//提交回复
function submitReply(request_url) {
	var parentid = $("#replyForm input:hidden[name=parentid]").val();
	var postid = $("#replyForm input:hidden[name=postid]").val();
	var content = $("#replyForm textarea[name=content]").val();
	
	if(content == '') {
		art.dialog.alert("你还未填写评论！");
		return false;
	}
	
	$.ajax({
        type:"POST",
        dataType:'json',
        url:request_url,
        data:{parentid:parentid,
        	  postid:postid,
        	  content:content},
        success:function(data){
        	if(data.status == '1') {
        		art.dialog({
	        	    icon: 'succeed',
	        	    content: data.info,
	        	    time:2
	        	});
        		//$('#join').load('__APP__?g={:GROUP_NAME}&m={:MODULE_NAME}&a=ajaxflush&postid='+postid);
        		location.reload(true);
        		//custom_data=data.data;
	        	//$('#test').load(custom_data.url);
        	} else {
        		art.dialog({
	        	    icon: 'error',
	        	    content: data.info,
	        	    time:2
	        	});
        	}
        },
        error:function(data){
        	art.dialog({
        	    icon: 'error',
        	    content: '操作失败！！！',
        	    time:2
        	});
        }
    });
}
		
//删除回复
function delete_reply(domid, request_url){
	art.dialog({
		icon:"warning",
	    content: '您确认要删除该评论吗？',
	    ok: function () {
	    	 $.ajax({
	    	        type:"POST",
	    	        dataType:'json',
	    	        url:request_url,
	    	        success:function(data){
	    	        	if(data.status == '1') {
	    	        		art.dialog({
		    	        	    icon: 'succeed',
		    	        	    content: data.info,
		    	        	    time:2
		    	        	});
	    	        		$("#"+domid).remove();
	    	        	} else {
	    	        		art.dialog({
		    	        	    icon: 'error',
		    	        	    content: data.info,
		    	        	    time:2
		    	        	});
	    	        	}
	    	        },
	    	        error:function(data){
	    	        	art.dialog({
	    	        	    icon: 'error',
	    	        	    content:'删除失败！！！',
	    	        	    time:2
	    	        	});
	    	        }
	    	    });
	    	 return true;
	    },
	    cancelVal: '取消',
	    cancel: true // 为true等价于function(){}
	});
};
		
//对回复点赞或取消赞
function zan_reply(domid, request_url) {
	$.ajax({
        type:"POST",
        dataType:'json',
        url:request_url,
        success:function(data){
        	if(data.status == '1') {
        		if(data.data.type == 'zan') {
        			$("#"+domid).html('<i class="icon icon-thumbs-up"></i>取消赞('+data.data.zan_num+')');
        		} else if(data.data.type == 'cancel_zan') {
        			$("#"+domid).html('<i class="icon icon-thumbs-up"></i>赞('+data.data.zan_num+')');
        		}
        	} else {
        		art.dialog({
	        	    icon: 'error',
	        	    content: data.info,
	        	    time:2
	        	});
        	}
        },
        error:function(data){
        	art.dialog({
        	    icon: 'error',
        	    content: '操作失败！',
        	    time:2
        	});
        }
    });
}